package com.kcht.itsed.mws.aleframework.facade;

import com.kcht.itsed.mws.aleframework.intf.IActiveSpecContainer;
import com.kcht.itsed.mws.aleframework.intf.IActiveSpecScheduler;
import com.kcht.itsed.mws.aleframework.intf.ILogicalReaderContainer;
import com.kcht.itsed.mws.aleframework.intf.ITagMemoryContainer;
import com.kcht.itsed.mws.extendmodel.jpa.LogicReaderEntity;
import com.kcht.itsed.mws.extendmodel.jpa.SubscriberEntity;
import com.kcht.itsed.mws.extendmodel.jpa.TagMemoryEntity;
import com.kcht.itsed.mws.persist.dao.IEventCycleDao;
import com.kcht.itsed.mws.persist.dao.ILogicReaderDao;
import com.kcht.itsed.mws.persist.dao.ISubscriberDao;
import com.kcht.itsed.mws.persist.dao.ITagMemoryDao;
import com.kcht.itsed.mws.subscribescheduler.facade.ISubscriberDeployer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 程序启动时加载各个规则
 * @author zhangwenshuo
 *
 */
@Component
@Slf4j
public class SpecLoaderOnStartup {

	
	@Autowired
	ITagMemoryDao tmDao;
	@Autowired
	ITagMemoryContainer tagMemoryContainer;
	
	@Autowired
	ILogicReaderDao lrDao;
	@Autowired
	ILogicalReaderContainer logicalReaderContainer;

	@Autowired
	IEventCycleDao ecDao;
	
	@Autowired
	IActiveSpecContainer activeSpecContainer;
	@Autowired
	ISubscriberDeployer subscriberDeployer;
	@Autowired
	IActiveSpecScheduler activeSpecScheduler;

	@Autowired
	ISubscriberDao subscriberDao;
	
	@PostConstruct
	public void loadSpecs() {
		log.info("Start to load all existed specs...");
		//加载现有的规则
		loadLRs();
		loadTMs();
		loadSubscribedECs();
		
		//开始阅读器动作
		activeSpecScheduler.shuffleSpec();
		log.info("Finished loading all existed specs");
	}
	
	private void loadLRs() {
		List<LogicReaderEntity> allLogicalReaderEntities = lrDao.loadAllLogicalReaderEntities();
		allLogicalReaderEntities.forEach(lrEntity->logicalReaderContainer.updateLRSpec(lrEntity.getLrName(),lrEntity.getLrSpec()));
	}
	
	private void loadTMs() {
		List<TagMemoryEntity> allTagMemoryEntities = tmDao.loadAllTagMemoryEntities();
		allTagMemoryEntities.forEach(tmEntity->tagMemoryContainer.updateTMSpec(tmEntity.getTmName(), tmEntity.getTmSpec())); 
	}
	
	private void loadSubscribedECs() {
		List<SubscriberEntity> allSubscriberEntities = subscriberDao.loadAllSubscriberEntities();
		//部署所有订阅者
		allSubscriberEntities.forEach(sub->{
			if(sub.getRelatedSpecType().equals("EC"))subscriberDeployer.deployEcSubscriber(sub.getRelatedSpec(), sub.getSubscriberUrl()); 
			else if(sub.getRelatedSpecType().equals("CC"))subscriberDeployer.deployCcSubscriber(sub.getRelatedSpec(), sub.getSubscriberUrl());
		});
		
		//部署所有活动EC订阅
		allSubscriberEntities.stream()
			.filter(sub->sub.getRelatedSpecType().equals("EC"))
			.map(sub->sub.getRelatedSpec())
			.collect(Collectors.toSet())
			.forEach(this::addActiveEC);
			
		//部署所有活动CC订阅，当前版本不支持CC规则，暂空 20180313
	}
	
	private void addActiveEC(String ecName) {
		ecDao.findBySpecName(ecName).ifPresent(ety->{
			activeSpecContainer.addActiveEC(ecName, ety.getEcSpec());
			ety.getEcSpec().getLogicalReaders().getLogicalReader()
				.forEach(lrName->logicalReaderContainer.bindActiveEcSpecToLr(ecName, lrName));
		});
	}

}
